$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'sys/buservipvalid/list',
        datatype: "json",
        colModel: [
            {
                label: '用户ID', name: 'userId',
                index: 'user_id',
                align: "center",
                width: 50,
                key: true,
                formatter: function (value, options, rowData) {
                    return "<a  onclick='window.open(\"../sys/buserinfo.html?userId=" + value + "\")'>" + value + "</a>";
                }
            },
            // {
            //     label: '用户编号',
            //     name: 'userId',
            //     index: 'user_id',
            //     align: "center",
            //     width: 50,
            //     key: true,
            //     formatter: function (value, options, rowData) {
            //         return "<a onclick=vm.editInfo(" + value + ")>" + value + "</a>";
            //     }
            // },
            {label: '用户昵称', name: 'nickName', index: 'nick_name', align: "center", width: 80},
            {label: '关注品牌', name: 'brandName', index: 'brand_name', align: "center", width: 80},
            {
                label: '用户头像',
                name: 'avatarUrl',
                index: 'avatar_url',
                width: 80,
                align: "center",
                sortable: false,
                editable: false,
                formatter: function (value, options, rowData) {
                    var url = "https://lktvtool.oss-cn-shenzhen.aliyuncs.com/icon/236A5FF0-52DE-4644-B66C-E9555C97693D.png";
                    if (value == null || value == "") {
                        return "<img src='" + url + "' width='60' />";
                    } else {
                        return "<img src='" + value + "' width='60' />";
                    }
                }
            },
            {label: '公众号', name: 'gzhName', index: 'gzhName', align: "center", width: 60},
            {label: '推荐人数', name: 'refererTotal', index: 'referer_total', align: "center", width: 80},
            {label: '文章分享数', name: 'shareTotal', index: 'share_total', align: "center", width: 80},
            {label: '开始时间', name: 'beginTime', index: 'begin_time', align: "center", width: 80},
            {
                label: '过期时间', name: 'endTime', index: 'end_time', align: "center", width: 80,
                formatter: function (value, options, rowData) {
                    let content = "<p style='color: red'>" + value + "</>"
                    return content;
                }
            },
            // {
            //     label: 'vip等级', name: 'vipLev', index: 'vip_lev', align: "center", width: 80,
            //     formatter: function (value, options, rowData) {
            //         var content = "";
            //         if (value == 1) {
            //             content = '半年';
            //         } else if (value == 2) {
            //             content = '一年';
            //         } else if (value == 3) {
            //             content = '三年';
            //         } else {
            //             content = "<p style='color: red'>" + '其他' + "</>"
            //         }
            //         return content;
            //     }
            // },
            {label: '会员状态', name: 'gendeStr', index: 'gendeStr', align: "center", width: 80},
            {label: '购买时间', name: 'createTime', index: 'create_time', align: "center", width: 80},
            {label: '最后活跃时间', name: 'lastActiveTime', index: 'last_active_time', align: "center", width: 80},
        ],
        viewrecords: true,
        height: 520,
        rowNum: 15,
        rowList: [15, 30, 50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth: true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader: {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        postData: {'referType': vm.referType, 'gzhCode': vm.gzhCode,},
        prmNames: {
            page: "page",
            rows: "limit",
            order: "order",
        },
        gridComplete: function () {
            //隐藏grid底部滚动条
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({"overflow-x": "hidden"});
        }
    });
    $.ajax({
        type: "POST",
        async: false,
        url: baseURL + 'sys/buserinfo/getUserGzhList',
        contentType: "application/json",
        success: function (r) {
            if (r.code == 0) {
                //vm.bbrandinfo=r.brandInfo;
                $("#selectUserGzhList").append(
                    "<option value=''>全部平台</option>");
                for (var i = 0; i < r.userGzhList.length; i++) {
                    $("#selectUserGzhList").append(
                        "<option value=" + r.userGzhList[i].gzhCode + ">" + r.userGzhList[i].gzhName + "</option>");
                }
            }
        }
    });
});

var vm = new Vue({
    el: '#rrapp',
    data: {
        showList: true,
        title: null,
        bUserVipValid: {},
        referType: "1",
        gzhCode: ''
    },
    methods: {
        query: function () {
            vm.reload();
        },
        add: function () {
            vm.showList = false;
            vm.title = "新增";
            vm.bUserVipValid = {};
        },
        update: function (event) {
            var userId = getSelectedRow();
            if (userId == null) {
                return;
            }
            vm.showList = false;
            vm.title = "修改";

            vm.getInfo(userId)
        },
        saveOrUpdate: function (event) {
            var url = vm.bUserVipValid.userId == null ? "sys/buservipvalid/save" : "sys/buservipvalid/update";
            $.ajax({
                type: "POST",
                url: baseURL + url,
                contentType: "application/json",
                data: JSON.stringify(vm.bUserVipValid),
                success: function (r) {
                    if (r.code === 0) {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    } else {
                        alert(r.msg);
                    }
                }
            });
        },
        del: function (event) {
            var userIds = getSelectedRows();
            if (userIds == null) {
                return;
            }

            confirm('确定要删除选中的记录？', function () {
                $.ajax({
                    type: "POST",
                    url: baseURL + "sys/buservipvalid/delete",
                    contentType: "application/json",
                    data: JSON.stringify(userIds),
                    success: function (r) {
                        if (r.code == 0) {
                            alert('操作成功', function (index) {
                                $("#jqGrid").trigger("reloadGrid");
                            });
                        } else {
                            alert(r.msg);
                        }
                    }
                });
            });
        },
        getInfo: function (userId) {
            $.get(baseURL + "sys/buservipvalid/info/" + userId, function (r) {
                vm.bUserVipValid = r.bUserVipValid;
            });
        },
        reload: function (event) {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam', 'page');
            console.info(vm.referType);
            $("#jqGrid").jqGrid('setGridParam', {
                postData: {'referType': vm.referType, "gzhCode": vm.gzhCode,},
                page: page,
            }).trigger("reloadGrid");
        },
        editInfo: function (ids, event) {
            vm.showList = false;
            vm.title = "修改";

            vm.getInfo(ids)
        },
        updateCensus: function (event) {

            $.ajax({
                type: "POST",
                url: baseURL + "sys/buserinfo/updateCensus",
                contentType: "application/json",
                success: function(r){
                    if(r.code === 0){
                        alert('操作成功', function(index){
                            vm.reload();
                        });
                    }else{
                        alert(r.msg);
                    }
                }
            });
        },
    }
});
